﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Announcement.Model;

namespace Announcement
{
    public partial class ApplyRead : LoginPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //取得表单数据
                ApplyTable at = new ApplyTable();
                HiddenField hfCCode = (HiddenField)fwApply.FindControl("hfCCode");
                at.ContractCode = hfCCode.Value;
                Label labName = (Label)fwApply.FindControl("ApplicantLabel");
                at.Applicant = labName.Text;
                Label labHouseLocal = (Label)fwApply.FindControl("HouseLocatedLabel");
                at.HouseLocated = labHouseLocal.Text;
                Label labHouseNum = (Label)fwApply.FindControl("HouseNumLabel");
                at.HouseNum = labHouseNum.Text;
                Label labApplyDate = (Label)fwApply.FindControl("ApplyDateLabel");
                at.ApplyDate = DateTime.Parse(labApplyDate.Text);
                Label labShifter = (Label)fwApply.FindControl("ShifterLabel");
                at.Shifter = labShifter.Text;
                Label labGiftCode = (Label)fwApply.FindControl("GiftCodeLabel");
                at.GiftCode = labGiftCode.Text;
                Label labGifter = (Label)fwApply.FindControl("GifterLabel");
                at.Gifter = labGifter.Text;
                Session["ApplyTable"] = at;
            }
        }

        protected void fwApply_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "Back")
            {
                Response.Redirect("ApplyList.aspx");
            }
            if (e.CommandName == "New")
            {
                Response.Redirect("Default.aspx");
            }
            if (e.CommandName == "EditGarage")
            {
                //取得合同编号
                HiddenField hfCCode = (HiddenField)fwApply.FindControl("hfCCode");
                string cCode = hfCCode.Value;
                string id = Request.QueryString["Id"];
                Response.Redirect("GarageRead.aspx?CCode=" + cCode + "&Id=" + id);
            }
            if (e.CommandName == "Announcement")
            {
                //跳转到相应打印界面
                RadioButtonList rblApplyType = (RadioButtonList)fwApply.FindControl("rblApplyType");
                int applyType = Convert.ToInt32(rblApplyType.SelectedValue);
                this.redirectPage(applyType);
            }
        }

        protected void fwApply_DataBound(object sender, EventArgs e)
        {
            //若公告类型为车库销售公告，则把编辑车位按钮显示出来
            RadioButtonList rblApplyType = (RadioButtonList)fwApply.FindControl("rblApplyType");
            string selectValue = rblApplyType.SelectedValue;
            if (selectValue == ApplyVar.ApplyType.GARAGE.ToString())
            {
                LinkButton garageButton = (LinkButton)fwApply.FindControl("GarageButton");
                garageButton.Visible = true;
                LinkButton aButton = (LinkButton)fwApply.FindControl("AnnouncementButton");
                aButton.Visible = false;
            }

        }

        private void redirectPage(int applyType)
        {
            switch (applyType)
            {
                case ApplyVar.ApplyType.OWNER:
                    Response.Redirect("Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.TOTAL:
                    Response.Redirect("Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.HIS:
                    Response.Redirect("Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.NOTICE:
                    Response.Redirect("Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.MORTGAGE:
                    Response.Redirect("Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.INITIAL:
                    Response.Redirect("Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.DEMOLITION:
                    Response.Redirect("Logout.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.LEGAL:
                    Response.Redirect("Logout.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.DIVORCE:
                    Response.Redirect("Shift.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.GARAGE:
                    Response.Redirect("Garage.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.PATCH:
                    Response.Redirect("Repair.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.GIFT:
                    Response.Redirect("GiftShift.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.OTHER:

                    break;
                default: break;
            }
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["UserName"] = null;
            Response.Redirect("~/Login.aspx");
        }
    }
}
